package com.kun.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kun.entity.Comment;
import com.kun.entity.User;
import com.kun.entity.Wishing;
import com.kun.result.ExceptionMsg;
import com.kun.result.Response;
import com.kun.result.ResponseData;
import com.kun.service.CommentService;
import com.kun.service.UserLikeCommentService;

/**
 * @description: 评论controller
 * @author: KM
 * 2018年4月10日
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController{
	
	@Autowired
	private CommentService commmentService;
	
	@Autowired
	private UserLikeCommentService userLikeCommentService;
	
	/**
	 * 获取图书评论总数
	 * */
	@RequestMapping("/getCount")
	@ResponseBody
	public Integer getCountComment(@RequestParam(name="bookId") int bookId){
		return commmentService.selectCountBookComment(bookId);
	}
	
	/**
	 * 获取图书发起评论
	 * */
	@RequestMapping("/getBookComment")
	@ResponseBody
	public List<Comment> getBookComment(@RequestParam(name="bookId") int bookId){
		Subject  currentUser = SecurityUtils.getSubject();
		User user = (User) currentUser.getPrincipal();
		List<Comment> resultList = new ArrayList<Comment>();
		Comment comment = new Comment();
		comment.setBookId(bookId);
		if(user!=null){
			resultList = commmentService.selectExampleComment(comment,user.getId());
		}else{
			resultList = commmentService.selectExampleComment(comment,null);
		}
		return resultList;
	}
	
	
	/**
	 * 获取许愿墙发起评论
	 * */
	@RequestMapping("/getWishComment")
	@ResponseBody
	public Map<String,Object> getWishComment(@RequestParam(name="begin") int begin,@RequestParam(name="length") int length,String jsonString){
		Subject  currentUser = SecurityUtils.getSubject();
		User user = (User) currentUser.getPrincipal();
		Map<String,Object> resultMap = new HashMap<String,Object>();
		if(user!=null){
			resultMap = commmentService.selectAllWish(begin, length, jsonString, user.getId());
		}else{
			resultMap = commmentService.selectAllWish(begin, length, jsonString, null);
		}
		return resultMap;
	}
	
	/**
	 * 获取评论的子评论
	 * */
	@RequestMapping("/getSonComment")
	@ResponseBody
	public List<Comment> getSonComment(@RequestParam(name="cid") int cid){
		Subject  currentUser = SecurityUtils.getSubject();
		User user = (User) currentUser.getPrincipal();
		List<Comment> resultList = new ArrayList<Comment>();
		if(user!=null){
			resultList = commmentService.selectSonComment(cid,user.getId());
		}else{
			resultList = commmentService.selectSonComment(cid,null);
		}
		return resultList;
	}
	
	/**
	 * prams:
	 * bookId
	 * blob 评论内容
	 * fatherId 如果是评论 其他用户发起的评论，需要传其id
	 * toName 如果是评论 发起评论底下的评论，需要传回复的用户名toName以及fatherId
	 * */
	@RequestMapping(value="/review",method=RequestMethod.POST)
	@ResponseBody
	public Response review(@RequestParam(name="bookId") int bookId,@RequestParam(name="blob") String blob,
			Integer fatherId,Integer toId){
		Subject  currentUser = SecurityUtils.getSubject(); 
		if(!currentUser.isAuthenticated()){
			return result(ExceptionMsg.MustToLogin);
		}
		User user = (User) currentUser.getPrincipal();
		if(user!=null&&bookId!=0&&blob!=null&&!"".equals(blob)){
			if(user.getState()==3){//禁言
				return result(ExceptionMsg.UserBanPost);
			}
			if(user.getAuthProcess()==0){//未认证
				return result(ExceptionMsg.UserNotAuth);
			}
			int comment_id = commmentService.insertComment(user.getId(), bookId, blob, fatherId, toId);
			Map<String,Object> returnMap = new HashMap<String,Object>();
			returnMap.put("comment_id", comment_id);
			returnMap.put("user", user);
			return new ResponseData(ExceptionMsg.SUCCESS,returnMap);
		}
		return result(ExceptionMsg.FAILED);
	}
	
	
	/**
	 * 许愿 包括评论许愿
	 * prams:
	 * blob 评论内容
	 * fatherId 如果是评论 其他用户发起的评论，需要传其id
	 * toName 如果是评论 发起评论底下的评论，需要传回复的用户名toName以及fatherId
	 * */
	@RequestMapping(value="/wishReview",method=RequestMethod.POST)
	@ResponseBody
	public Response wishReview(@RequestParam(name="blob") String blob,
			Integer fatherId,Integer toId,Wishing wish){
		Subject  currentUser = SecurityUtils.getSubject(); 
		if(!currentUser.isAuthenticated()){
			return result(ExceptionMsg.MustToLogin);
		}
		User user = (User) currentUser.getPrincipal();
		if(user!=null&&blob!=null&&!"".equals(blob)){
			if(user.getState()==3){//禁言
				return result(ExceptionMsg.UserBanPost);
			}
			if(user.getAuthProcess()==0){//未认证
				return result(ExceptionMsg.UserNotAuth);
			}
			Map<String,Object> map  = commmentService.insertWishComment(user.getId(), wish, blob, fatherId, toId);
			Map<String,Object> returnMap = new HashMap<String,Object>();
			returnMap.put("comment_id", map.get("commentId"));
			returnMap.put("wish_id", map.get("wishId"));
			returnMap.put("user", user);
			return new ResponseData(ExceptionMsg.SUCCESS,returnMap);
		}
		return result(ExceptionMsg.FAILED);
	}
	
	
	/**
	 * 用户删除个人评论
	 * */
	@RequiresAuthentication
	@RequestMapping(value="/deleteComment",method=RequestMethod.POST)
	@ResponseBody
	public Response deleteComment(@RequestParam(name="commentId") int commentId,Integer fatherId){
		Subject  currentUser = SecurityUtils.getSubject(); 
		User user = (User) currentUser.getPrincipal();
		if(user!=null){
			int i = commmentService.deleteMyComment(commentId, fatherId, user.getId());
			if(i>0){
				return result(ExceptionMsg.SUCCESS);
			}
		}
		return result(ExceptionMsg.FAILED);
	}
	
	/**
	 * 用户点赞
	 * */
	@RequestMapping(value="/toLike",method=RequestMethod.POST)
	@ResponseBody
	public Response toLike(int commentId,String likeValue){
		Subject  currentUser = SecurityUtils.getSubject();
		User user = (User) currentUser.getPrincipal();
		if(user!=null){
			int i = userLikeCommentService.addSet(user.getId(), commentId, likeValue);
			if(i!=1){
				return result(ExceptionMsg.FAILED);
			}
			return result(ExceptionMsg.SUCCESS);
		}
		return result(ExceptionMsg.FAILED);
	}
}
